﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace WindowsFormProject.Depo
{
    public class UrunTransferIslemleri
    {
        public class StokHareketTut
        {
            public int StokHaraketID { get; set; }
            public string UrunId { get; set; }
            public string UrunADi { get; set; }
            public decimal StokHaraketAdet { get; set; }
            public int StokHaraketGonderenSubeID { get; set; }
            public string StokHaraketGonderenSubeAdi { get; set; }
            public int StokHaraketAlanSubeID { get; set; }
            public string StokHaraketAlanSubeAdi { get; set; }
            public string StokHaraketTarih { get; set; }
            public string StokHaraketHaraketTuru { get; set; }
            public string StokHaraketIrsaliyeNo { get; set; }
            public override string ToString()
            {
                return StokHaraketID + "" + UrunId + "" + UrunADi + "" + StokHaraketAdet + "" + StokHaraketGonderenSubeID + "" + StokHaraketGonderenSubeAdi + "" + StokHaraketAlanSubeID + "" + StokHaraketAlanSubeAdi + "" + StokHaraketTarih + "" + StokHaraketHaraketTuru + "" + StokHaraketIrsaliyeNo;
            }
        }
        public class StokHareketDetayIslemler : IEnumerable
        {
            DataBaseProvider MyDataBase = new DataBaseProvider();
            List<StokHareketTut> StokHareketDetayList = new List<StokHareketTut>();
            

            public DataTable StokHareketleri(int StokHaraketID)
            {
                DataTable ReturnValue;
                MyDataBase.AddInParameter("@StokHaraketID", SqlDbType.Int, StokHaraketID);
                ReturnValue = MyDataBase.ExecuteDataTable("select SH.*,UrunADi From StokHaraketleri  as SH Left Outer Join	 dbo.MErkezUrunler	MU ON MU.UrunId=SH.UrunID WHERE	SH.StokHaraketID  =	@StokHaraketID", CommandType.Text,"StokHaraketleri");
                MyDataBase.ClearParameters();
                return ReturnValue;
            }

            public DataTable StokHareketleri(string StokHaraketIrsaliyeNo)
            {
                DataTable ReturnValue;
                MyDataBase.AddInParameter("@StokHaraketIrsaliyeNo", SqlDbType.NVarChar, StokHaraketIrsaliyeNo);
                ReturnValue = MyDataBase.ExecuteDataTable("select SH.UrunID,MU.UrunADi,SH.StokHaraketAdet From StokHaraketleri  as SH Left Outer Join	 dbo.MErkezUrunler	MU ON MU.UrunId=SH.UrunID WHERE	SH.StokHaraketIrsaliyeNo  =	@StokHaraketIrsaliyeNo", CommandType.Text,"StokHaraketleri");
                MyDataBase.ClearParameters();
                return ReturnValue;
            }

            public decimal AdetTopla(string StokHaraketIrsaliyeNo)
            {
                decimal Toplam;
                MyDataBase.AddInParameter("@StokHaraketIrsaliyeNo", SqlDbType.NVarChar, StokHaraketIrsaliyeNo);
                Toplam =MyDataBase.ExecuteNonQuery("select sum(SH.StokHaraketAdet) From StokHaraketleri  as SH WHERE	SH.StokHaraketIrsaliyeNo  =	@StokHaraketIrsaliyeNo", CommandType.Text);
                MyDataBase.ClearParameters();
                return Toplam;            
            }
            public List<StokHareketTut> KayitIdyeGore(int recID)
            {
                List<StokHareketTut> SecilenUrun = new List<StokHareketTut>();

                foreach (StokHareketTut item in StokHareketDetayList)
                {
                    if (item.StokHaraketID == recID)
                    {
                        SecilenUrun.Add(item);
                    }
                }

                return SecilenUrun;
            }

            public void StokHareketKayitEkle(StokHareketTut S)
            {
                StokHareketDetayList.Add(S);

            }
            public void StokHareketTutClear(StokHareketTut S)
            {
                StokHareketDetayList.Clear();
            }

            public int Get_RecordCount(string StokHaraketIrsaliyeNo)
            {
                int ReturnValue = 0;
                MyDataBase.AddInParameter("@StokHaraketIrsaliyeNo", SqlDbType.NVarChar, StokHaraketIrsaliyeNo);
                ReturnValue = Convert.ToInt32(MyDataBase.ExecuteScalar("Select Count(StokHaraketID) from StokHaraketleri where StokHaraketIrsaliyeNo = @StokHaraketIrsaliyeNo", CommandType.Text));
                MyDataBase.ClearParameters();
                return ReturnValue;
            }
            public int StokHareketDelete(int StokHaraketID)
           {
               int ReturnValue = 0;
               MyDataBase.AddInParameter("@StokHaraketID", SqlDbType.Int, StokHaraketID);
               ReturnValue = Convert.ToInt32(MyDataBase.ExecuteNonQuery("Delete  StokHaraketleri where StokHaraketID = @StokHaraketID",CommandType.Text));
               MyDataBase.ClearParameters();
               return ReturnValue;           
           }
            public int StokHareketVeriTabaninaKaydet(StokHareketTut S)
            {
                int ReturnValue = 0;
                int ToplamEtkilenenKayitSayisi = 0;
                foreach (StokHareketTut item in StokHareketDetayList)
                {
                    try
                    {
                        MyDataBase.AddInParameter("@StokHaraketID", SqlDbType.Int, S.StokHaraketID);
                        MyDataBase.AddInParameter("@UrunID", SqlDbType.NVarChar, S.UrunId);
                        MyDataBase.AddInParameter("@StokHaraketAdet", SqlDbType.Decimal, S.StokHaraketAdet);
                        MyDataBase.AddInParameter("@StokHaraketGonderenSubeID", SqlDbType.Int, S.StokHaraketGonderenSubeID);
                        MyDataBase.AddInParameter("@StokHaraketAlanSubeID", SqlDbType.Int, S.StokHaraketAlanSubeID);
                        MyDataBase.AddInParameter("@StokHaraketTarih", SqlDbType.DateTime, S.StokHaraketTarih);
                        MyDataBase.AddInParameter("@StokHaraketHaraketTuru", SqlDbType.NVarChar, S.StokHaraketHaraketTuru);
                        MyDataBase.AddInParameter("@StokHaraketIrsaliyeNo", SqlDbType.NVarChar, S.StokHaraketIrsaliyeNo);
                        MyDataBase.ExecuteNonQuery("insert into StokHaraketleri (UrunId,StokHaraketAdet,StokHaraketGonderenSubeID,StokHaraketAlanSubeID,StokHaraketTarih,StokHaraketHaraketTuru,StokHaraketIrsaliyeNo)values(@UrunId,@StokHaraketAdet,@StokHaraketGonderenSubeID,@StokHaraketAlanSubeID,@StokHaraketTarih,@StokHaraketHaraketTuru,@StokHaraketIrsaliyeNo)", CommandType.Text);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    MyDataBase.ClearParameters();
                    return ReturnValue;
                }
                return ToplamEtkilenenKayitSayisi;
            }
            public int StokHareketVeriTabaniKayitGuncelle(StokHareketTut S)
            {
                int ReturnValue = 0;
                int ToplamEtkilenenKayitSayisi = 0;
                foreach (StokHareketTut item in StokHareketDetayList)
                {
                    try
                    {
                        MyDataBase.AddInParameter("@StokHaraketID", SqlDbType.Int, S.StokHaraketID);
                        MyDataBase.AddInParameter("@UrunID", SqlDbType.NVarChar, S.UrunId);
                        MyDataBase.AddInParameter("@StokHaraketAdet", SqlDbType.Decimal, S.StokHaraketAdet);
                        MyDataBase.AddInParameter("@StokHaraketGonderenSubeID", SqlDbType.Int, S.StokHaraketGonderenSubeID);
                        MyDataBase.AddInParameter("@StokHaraketAlanSubeID", SqlDbType.Int, S.StokHaraketAlanSubeID);
                        MyDataBase.AddInParameter("@StokHaraketTarih", SqlDbType.DateTime, S.StokHaraketTarih);
                        MyDataBase.AddInParameter("@StokHaraketHaraketTuru", SqlDbType.NVarChar, S.StokHaraketHaraketTuru);
                        MyDataBase.AddInParameter("@StokHaraketIrsaliyeNo", SqlDbType.NVarChar, S.StokHaraketIrsaliyeNo);
                        MyDataBase.ExecuteNonQuery("Update StokHaraketleri set UrunId = @UrunId,StokHaraketAdet=@StokHaraketAdet,StokHaraketGonderenSubeID=@StokHaraketGonderenSubeID,StokHaraketAlanSubeID=@StokHaraketAlanSubeID,StokHaraketTarih=@StokHaraketTarih,StokHaraketHaraketTuru=@StokHaraketHaraketTuru,StokHaraketIrsaliyeNo=@StokHaraketIrsaliyeNo where StokHaraketID=@StokHaraketID  ", CommandType.Text);
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                    MyDataBase.ClearParameters();
                    return ReturnValue;
                }
                return ToplamEtkilenenKayitSayisi;
            }
            public IEnumerator GetEnumerator()
            {
                return new Gezdirici(StokHareketDetayList);
            }
        }
        public class Gezdirici:IEnumerator
        {
            List<StokHareketTut> GelenListe;
            int ToplamEleman;
            int GecerliElemanNo;
            public  Gezdirici(List<StokHareketTut>liste)
            {
                GelenListe = liste;
                ToplamEleman = liste.Count;
                GecerliElemanNo = -1;
            }
            public object Current
            {
                get
                {
                    return GelenListe[GecerliElemanNo];
                }
            }

            public bool MoveNext()
            {

                GecerliElemanNo++;
                if (GecerliElemanNo < ToplamEleman)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            public void Reset()
            {

                GecerliElemanNo = -1;
            }

        }


    }
}
